const express = require('express');
const router = express.Router();
const db = require("../../../util/dbConfig/dbConfig")

router.post('/',async (req,res) => {
//管理员可以修改全部信息
    let {id,newId,userName,phone,town,village,community,term} = req.body
    //参数必须都要有
    if(!id ||!newId || !userName || !phone || !town || !village || !community || !term){
        return res.send({
            code: 1,
            msg: '参数错误，请输入修改信息'
        })
    }
    try{
        //修改用户表的信息
        //需要前端将地址名称对应的id发来
        //与前端拟定好，地址id与地址名的对应规则
        let sqlUser = `update user set ID=?,PHONENUM=?,NAME=?,TOWN_ID=?,VILLAGE_ID=?,COMMUNITY_ID=?,term_id=? where ID=?`
        let sqlArr = [newId,phone,userName,town,village,community,term,id]
        let resultUser = await db.SySqlConnect(sqlUser,sqlArr)
        console.log('resultUser',resultUser.changedRows)
        if( !resultUser.changedRows){
            return res.send({
                code: 2,
                msg: '修改失败'
            })
        }else if( resultUser.changedRows === 0){
            return res.send({
                code: 1,
                msg: '参数重复，请客户端修复'
            })
        }else if( resultUser.changedRows === 1){
            return res.send({
                code: 0,
                msg: '修改成功'
            })
        }else{
            return res.send({
                code: 1,
                msg: '参数错误，请客户端修复'
            })
        }

    }catch (e) {
        console.log(e);
        return res.send({
            code: 4,
            msg: '服务器错误，请稍后再试'
        })
    }
})

module.exports = router
